<?php

use Doraemon\tools\HhzClient;
use Biz\service\trade\UserAddress as sAddressUser;

class SaveAction extends ActionPlugin
{
    const USER_LEVEL = self::LEVEL_REGISTER;

    /**
     * @throws Exception
     */
    public function main()
    {
        $addressId = (int)$this->getRequest()->getPost('id', 0);
        $data = [
            'uid' => $this->uid,
            'accept_name' => $this->getRequest()->getPost('accept_name', ''),
            'info_address' => $this->getRequest()->getPost('info_address', ''),
            'can_enter_community' => $this->getRequest()->getPost('can_enter_community', '-1'),
            'have_elevator' => $this->getRequest()->getPost('have_elevator', '-1'),
            'which_floor' => $this->getRequest()->getPost('which_floor', '0'),
        ];
        $mobile = $this->getRequest()->getPost('mobile', '');
        $data['mobile'] = $mobile ? trim($mobile) : '';
        $data['area_code'] = $this->getRequest()->getPost('area_code', '');
        if (!$data['accept_name'] || !$data['area_code'] || !$data['info_address'] || !$data['mobile']) {
            throw new Exception('收货地址信息不完整，请补全信息', self::CODE_ERROR_VIEW_TOSTA);
        }
        $result = sAddressUser::saveOrEdit($addressId, $data);

        $result['address_id'] = $result['id'];
        $this->setSuccess($result);
    }
}